﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using Wpf_ClientLourd.ServiceBP;
using Wpf_ClientLourd.BusinessObject;

namespace Wpf_ClientLourd
{
    /// <summary>
    /// Logique d'interaction pour ImpressionBpDate.xaml
    /// </summary>
    public partial class ImpressionBpDate : UserControl
    {
        ServiceBP.ServiceBPClient clientBP;
        DateTime dt_deb;
        DateTime dt_fin;

        public ImpressionBpDate()
        {
            InitializeComponent();
            clientBP = new ServiceBP.ServiceBPClient();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Récupération des dates sélectionnées
            this.dt_deb = new DateTime(dateDebut.SelectedDate.Value.Year, dateDebut.SelectedDate.Value.Month, dateDebut.SelectedDate.Value.Day, 0, 0, 0);
            this.dt_fin = new DateTime(dateFin.SelectedDate.Value.Year, dateFin.SelectedDate.Value.Month, dateFin.SelectedDate.Value.Day, 0, 0, 0);

            //Génération du fichier pdf
            Document pdfBP = new Document();
            string path = @"C:\Users\Matthieu\Desktop\";
            try
            {
                PdfWriter.GetInstance(pdfBP, new FileStream(path + "ListeBP2.pdf", FileMode.Create, FileAccess.ReadWrite));
                pdfBP.Open();

                //Création tableau
                PdfPTable table = new PdfPTable(6);
                PdfPCell cell = new PdfPCell(new Phrase(" Liste des BP à éditer "));
                cell.Colspan = 6;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
                table.AddCell("BP");
                table.AddCell("N° BP");
                table.AddCell("date BP");
                table.AddCell("Statut");
                table.AddCell("Commande");
                table.AddCell("Type");

                int cptBP = 0;
                //Récupération de la liste des BP
                List<BP> collectionBP = new List<BP>();
                foreach (BPDTO bpdto in clientBP.GetBPByDate(this.dt_deb,this.dt_fin).ToList())
                {
                    BP bp = new BP(bpdto);
                    collectionBP.Add(bp);
                }
                foreach (BP bP in collectionBP)
                {
                    
                    //On remplit la ligne du BP
                    table.AddCell((cptBP + 1).ToString());
                    table.AddCell(bP.id.ToString());
                    table.AddCell(bP.date.ToShortDateString());
                    table.AddCell(bP.statut);
                    table.AddCell(bP.id_cmd.ToString());
                    table.AddCell(bP.type);

                    //On créé la ligne d'entete pour les lignes de BP
                    table.AddCell("Ligne BP");
                    table.AddCell("Identifiant");
                    table.AddCell("Quantité");
                    table.AddCell("Emplacement");
                    table.AddCell("Unité");
                    table.AddCell("Statut");


                    //Appel du web service pour récupérer les lignes de BP
                    List<LgBpDTO> Listlgbp = clientBP.GetLgBPByBP(bP.id).ToList();
                    //On remplit les lignes du tableau avec les informations
                    for (int i = 0; i < Listlgbp.Count(); i++)
                    {

                        LgBpDTO lg = Listlgbp.ElementAt(i);

                        //On appelle le webService pour récuperer les informations sur les emplacements
                        EmplacementDTO emp = clientBP.GetEmplacementByID(lg.idEmplacement);

                        table.AddCell(lg.numeroLigne.ToString());
                        table.AddCell(lg.id.ToString());
                        table.AddCell(lg.qteAPrepa.ToString());
                        table.AddCell(emp.libEmplacement);

                        switch (lg.codeUnite)
                        {
                            case "PAL": table.AddCell("Palette"); break;
                            case "P": table.AddCell("Picking"); break;
                        }

                        switch (lg.statut)
                        {
                            case 0: table.AddCell("Générée"); break;
                            case 1: table.AddCell("Reliquat"); break;
                            case 2: table.AddCell("En cours"); break;
                            case 3: table.AddCell("Clôturée"); break;
                        }
                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString(), "Erreur Génération PDF", MessageBoxButton.OK);
            }
        }

        private void dateDebut_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dateFin.SelectedDate !=null && dateDebut.SelectedDate != null)
            {
                bt_gen.IsEnabled = true;
            }
        }

        private void dateFin_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dateFin.SelectedDate != null && dateDebut.SelectedDate != null)
            {
                bt_gen.IsEnabled = true;
            }
        }
    }
}
